{% extends "base.html" %}
{% load i18n %}
{% load display_tags %}
{% block add_styles %}
    {{ block.super }}
    form.biweekly-metrics p { display: inline-block;}
    form.biweekly-metrics input {border: 1px solid #ccc;border-radius: 4px;padding: 3px 6px;}
    form.biweekly-metrics select {border-radius: 4px;background-color: #fff;background-image: none;border: 1px solid
    #ccc;height: 27px;}
    .errorlist {color: #a94442; display: inline-block;}
{% endblock %}
{% block content %}
    {{ block.super }}

    <form class="biweekly-metrics" method="get">
        {{ form.as_p }}
        <input class="btn btn-sm btn-primary" type="submit" value="{% trans "Generate Metrics For Selected Period" %}"/>
    </form>
    <br/>
    {% if pt %}
        <h2>{% blocktrans with start_date=start_date.date end_date=end_date.date%}Finding Information For Period of {{ start_date }} - {{ end_date }}
            {% endblocktrans %}</h2>
        <h3 class="inline-block">{{ pt.name }}</h3>
        {% if pt|class_name == "Product_Type" %}
        [<a href="{% url 'product_type_metrics' pt.id %}" class="inline-block">{% trans "View Details" %}</a>]
        {% elif pt|class_name == "Tagulous_Product_tags" %}
        [<a href="{% url 'product' %}?tags={{ pt }}" class="inline-block">{% trans "View Details" %}</a>]
        {% endif %}
        <div class="panel panel-default table-responsive">
            <div class="panel-heading">
                <h4>{% trans "Total Security Bug Count In Period" %}</h4>
            </div>
            <table class="table table-bordered table-condensed table-striped">
                <thead>
                <tr>
                    <th>{% trans "Critical" %}</th>
                    <th>{% trans "High" %}</th>
                    <th>{% trans "Medium" %}</th>
                    <th>{% trans "Low" %}</th>
                    <th>{% trans "Total" %}</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td class="text-right">
                        {{ overall_in_pt.S0|default_if_none:0 }}
                    </td>
                    <td class="text-right">
                        {{ overall_in_pt.S1|default_if_none:0 }}
                    </td>
                    <td class="text-right">
                        {{ overall_in_pt.S2|default_if_none:0 }}
                    </td>
                    <td class="text-right">
                        {{ overall_in_pt.S3|default_if_none:0 }}
                    </td>
                    <td class="text-right">
                        {{ overall_in_pt.Total|default_if_none:0 }}
                    </td>
                </tr>
                </tbody>
            </table>
        </div>

        <br/>
        <div class="panel panel-default table-responsive">
            <div class="panel-heading">
                <h4>{% trans "Total Security Bugs Opened In Period" %}</h4>
            </div>
            <table class="table table-bordered table-condensed table-striped">
                <thead>
                <tr>
                    <th>{% trans "Critical" %}</th>
                    <th>{% trans "High" %}</th>
                    <th>{% trans "Medium" %}</th>
                    <th>{% trans "Low" %}</th>
                    <th>{% trans "Total" %}</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td class="text-right">
                        {{ opened_in_period.S0|default_if_none:0 }}
                    </td>
                    <td class="text-right">
                        {{ opened_in_period.S1|default_if_none:0 }}
                    </td>
                    <td class="text-right">
                        {{ opened_in_period.S2|default_if_none:0 }}
                    </td>
                    <td class="text-right">
                        {{ opened_in_period.S3|default_if_none:0 }}
                    </td>
                    <td class="text-right">
                        {{ opened_in_period.Total|default_if_none:0 }}
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
        <br/>
        <div class="panel panel-default table-responsive">
            <div class="panel-heading">
                <h4>{% trans "Total Security Bugs Closed In Period" %}</h4>
            </div>
            <table class="table table-bordered table-condensed table-striped">
                <thead>
                <tr>
                    <th>{% trans "Critical" %}</th>
                    <th>{% trans "High" %}</th>
                    <th>{% trans "Medium" %}</th>
                    <th>{% trans "Low" %}</th>
                    <th>{% trans "Total" %}</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td class="text-right">
                        {{ closed_in_period.S0|default_if_none:0 }}
                    </td>
                    <td class="text-right">
                        {{ closed_in_period.S1|default_if_none:0 }}
                    </td>
                    <td class="text-right">
                        {{ closed_in_period.S2|default_if_none:0 }}
                    </td>
                    <td class="text-right">
                        {{ closed_in_period.S3|default_if_none:0 }}
                    </td>
                    <td class="text-right">
                        {{ closed_in_period.Total|default_if_none:0 }}
                    </td>
                </tr>
                </tbody>
            </table>
        </div>

        <br/>
        <div class="panel panel-default table-responsive">
            <div class="panel-heading">
                <h4>{% trans "Trending Total Bug Count By Month" %}</h4>
            </div>
            <table class="table table-bordered table-condensed table-striped">
                <thead>
                <tr>
                    <th>{% trans "Month" %}</th>
                    <th>{% trans "Critical" %}</th>
                    <th>{% trans "High" %}</th>
                    <th>{% trans "Medium" %}</th>
                    <th>{% trans "Low" %}</th>
                    <th>{% trans "Opened in Month" %}</th>
                    <th>{% trans "Open to Date" %}</th>
                    <th>{% trans "Total Closed" %}<sup>*</sup></th>
                </tr>
                </thead>
                <tfoot style="font-size: 12px;" class="text-info">
                <tr>
                    <td colspan="8"><sup>*</sup>{% trans "Closed findings may have been opened outside of requested period." %}</td>
                </tr>
                </tfoot>
                <tbody>
                {% for to in trending_opened %}
                    <tr>
                        <td>{{ to.start_date.date|date:"M-Y" }}</td>
                        <td class="text-right">
                            {{ to.S0|default_if_none:0 }}
                        </td>
                        <td class="text-right">
                            {{ to.S1|default_if_none:0 }}
                        </td>
                        <td class="text-right">
                            {{ to.S2|default_if_none:0 }}
                        </td>
                        <td class="text-right">
                            {{ to.S3|default_if_none:0 }}
                        </td>
                        <td class="text-right">
                            {{ to.Total|default_if_none:0 }}
                        </td>
                        <td class="text-right">
                            {{ to.to_date_total|default_if_none:0 }}
                        </td>
                        <td class="text-right">
                            {{ to.closed|default_if_none:0 }}
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>

        <br/>
        <div class="panel panel-default table-responsive">
            <div class="panel-heading">
                <h4>{% trans "Top 10 By Bug Severity" %}</h4>
            </div>
            <table class="table table-bordered table-condensed table-striped">
                <thead>
                <tr>
                    <th>{{ labels.ASSET_LABEL }}</th>
                    <th>{% trans "Critical" %}</th>
                    <th>{% trans "High" %}</th>
                    <th>{% trans "Medium" %}</th>
                    <th>{% trans "Low" %}</th>
                    <th>{% trans "Total" %}</th>
                </tr>
                </thead>
                <tbody>
                {% for p in top_ten %}
                    <tr>
                        <td>
                            {{ p.name }}
                        </td>
                        <td class="text-right">
                            {{ p.critical|default_if_none:0 }}
                        </td>
                        <td class="text-right">
                            {{ p.high|default_if_none:0 }}
                        </td>
                        <td class="text-right">
                            {{ p.medium|default_if_none:0 }}
                        </td>
                        <td class="text-right">
                            {{ p.low|default_if_none:0 }}
                        </td>
                        <td class="text-right">
                            {{ p.total|default_if_none:0 }}
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
        <br/>
        <div class="panel panel-default table-responsive">
            <div class="panel-heading">
                <h4>{% blocktrans %}{{ pt }} Open Findings{% endblocktrans %}</h4>
            </div>
            <table id="open_findings"
                   class="tablesorter-bootstrap table table-bordered table-condensed table-striped table-hover">
                <thead>
                <tr>
                    <th>{% trans "No." %}</th>
                    <th>{% trans "Name" %}</th>
                    <th>{% trans "Date" %}</th>
                    <th>{% trans "Severity" %}</th>
                    <th>{% trans "EPSS Score / Percentile" %}</th>
                    <th>{% trans "Age" %}</th>
                    <th>{{ labels.ASSET_LABEL }}</th>
                </tr>
                </thead>
                <tbody>
                {% for finding in all_current_in_pt %}
                    <tr>
                        <td class="text-right">{{ forloop.counter }}</td>
                        <td><a title="{{ finding.title }}"
                               href="{% url 'view_finding' finding.id %}">{{ finding.title }}</a>
                        </td>
                        <td class="nowrap">{{ finding.date }}</td>
                        <td>{% if finding.severity == "Critical" or finding.severity == "High" %}
                            <p class="text-danger">
                        {% else %}<p>{% endif %}{{ finding.severity_display }}</p></td>
                        <td class="text-right">
                            {{ finding.epss_score|format_epss }}
                            /
                            {{ finding.epss_percentile|format_epss }}
                        </td>
                        <td class="text-right">{{ finding.age }}</td>
                        <td><a
                                href="{% url 'view_product' finding.test.engagement.product.id %}"
                                title="{{ finding.test.engagement.product }}">{{ finding.test.engagement.product }}</a>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        </div>
    {% endif %}

{% endblock %}